package com.qyer.search.model.aggregation;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by tangzheng on 16/7/17.
 */
public class AggregationProduct {
  @JsonProperty
  private Map<Integer, STGInfo> stg_info;


  public AggregationProduct(Map<Integer, STGInfo> stg_info) {
    this.stg_info = stg_info;
  }

  public AggregationProduct() {
    stg_info = new HashMap<>();
  }

  public Map<Integer, STGInfo> getStg_info() {
    return stg_info;
  }

  public void setStg_info(Map<Integer, STGInfo> stg_info) {
    this.stg_info = stg_info;
  }

  public void add(AggregationProduct aggregationProduct){
    if(aggregationProduct != null) {
      if (stg_info != null) {
        Iterator<Map.Entry<Integer, STGInfo>> it = stg_info.entrySet().iterator();
        while (it.hasNext()) {
          Map.Entry<Integer, STGInfo> entry = it.next();
          int a = entry.getKey();
          if (aggregationProduct.getStg_info().containsKey(a)){
            STGInfo stgInfo = aggregationProduct.getStg_info().get(a);
            aggregationProduct.getStg_info().remove(a);
            STGInfo stgInfoA = entry.getValue();
            stgInfoA.add(stgInfo);
          }
        }
        //
        if(aggregationProduct.getStg_info() != null){
          stg_info.putAll(aggregationProduct.getStg_info());
        }
      } else {
        stg_info = aggregationProduct.getStg_info();
      }
    }
  }

  public void sort(){
    Iterator<Map.Entry<Integer, STGInfo>> it = stg_info.entrySet().iterator();
    while(it.hasNext()){
      Map.Entry<Integer, STGInfo> entry = it.next();
      STGInfo stgInfo = entry.getValue();
      stgInfo.sort();
      stgInfo.transfer();
    }
  }

}
